/**
 * Created by cyf on 15-12-1.
 * 好友树构建器
 */
(function() {
	qxchat.OrgTree = OrgTree;
	var EXTEND = null; //是否需要继承
	function OrgTree(treeViewId, zNodes) {
		if (EXTEND) {
			EXTEND.apply(this, arguments);
		}
		// 初始化菜单
		this.init();
		this.zTreeObj = null;
		this.orgTreeRMenu = $("#orgTreeRMenu");
		this.currentNode = null;
		var self = this;
		var setting = {
			view: {
				showLine: false,
				showIcon: false,
				selectedMulti: false,
				dblClickExpand: true,
				addDiyDom: addDiyDom
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onClick: onClick,
				onRightClick: OnRightClick
			}
		};
		//点击事件
		function onClick(event, treeId, treeNode, clickFlag) {
			//是否可点击，可理解为是否为用户节点，如果是打开窗口
			if (!(treeNode.click == false)) {
				global.chartWinHandler.open(treeNode.loginName, treeNode.name)
			} else {
				//self.zTreeObj.expandNode(treeNode)
			}
		}

		function OnRightClick(event, treeId, treeNode) {
			self.currentNode = treeNode;
			if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
				self.zTreeObj.cancelSelectedNode();
				showOrgTreeRMenu("root", event.clientX, event.clientY);
			} else if (treeNode && !treeNode.noR && !treeNode.isParent) {
				self.zTreeObj.selectNode(treeNode);
				showOrgTreeRMenu("node", event.clientX, event.clientY);
			}
		}

		//联系人添加到分组
		this.firednAddToBand = function(e) {
			hideOrgTreeRMenu();
			var bandId = $(this).attr("id")
			var treeNode = global.personalBandDataTreeObj.zTreeObj.getNodeByParam("id", bandId);
			global.personalBandDataTreeObj.zTreeObj.addNodes(treeNode, self.currentNode);
			//写到后台
			var message = '{"data":{"loginName":"' + global.currentUserLoginName + '","bandId":"' + treeNode.id + '","memberLoginName":"' + self.currentNode.loginName + '","type":"BandAddMember",},"type":"event"}';
			global.webSocketHandler.send(message);
		}

		function showOrgTreeRMenu(type, x, y) {
			if (type == "node") {
				$("#orgTreeRMenu ul").show();
				var width = self.orgTreeRMenu.width();
				var height = self.orgTreeRMenu.height();
				if ((x + width) > document.body.offsetWidth) {
					x -= (x + width) - document.body.offsetWidth + 10;
				}
				if ((y + height) > document.body.offsetHeight) {
					y -= (y + height) - document.body.offsetHeight + 10;
				}
				self.orgTreeRMenu.css({
					"top": y + "px",
					"left": x + "px",
					"visibility": "visible"
				});

				$("body").bind("mousedown", onBodyMouseDown);
			}
		}

		function hideOrgTreeRMenu() {
			if (self.orgTreeRMenu) self.orgTreeRMenu.css({
				"visibility": "hidden"
			});
			$("body").unbind("mousedown", onBodyMouseDown);
		}

		function onBodyMouseDown(event) {
			if (!(event.target.id == "orgTreeRMenu" || $(event.target).parents("#orgTreeRMenu").length > 0)) {
				self.orgTreeRMenu.css({
					"visibility": "hidden"
				});
			}
		}
		//初始化联系人样式
		function addDiyDom(treeId, treeNode) {
			var spaceWidth = 5;
			var switchObj = $("#" + treeNode.tId + "_switch")
			var icoObj = $("#" + treeNode.tId + "_ico");
			var text = $("#" + treeNode.tId + "_span");
			var spaceStr = "<span id='" + treeNode.tId + "_span_status' style='padding-left:15px'></span>";
			text.append(spaceStr);
			var ustatus = $("#" + treeNode.tId + "_span_status");
			switchObj.remove();
			icoObj.before(switchObj);

			if (treeNode.level > 1) {
				var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level) + "px'></span>";
				switchObj.before(spaceStr);
			}
			switch (treeNode.status) {
				case 0:
					ustatus.css("color", "gray");
					ustatus.html("离线");
					break;
				case 1:
					ustatus.css("color", "green");
					ustatus.html("在线");
					break;
			}
		}
		var treeObj = $("#" + treeViewId);
		//构建树
		$.fn.zTree.init(treeObj, setting, zNodes);
		treeObj.addClass("showIcon");
		//当前zTree对象
		this.zTreeObj = $.fn.zTree.getZTreeObj(treeViewId);
		this.zTreeObj.expandAll(true);
		this.zTreeObj.expandAll(false);
	}
	OrgTree.prototype = {
		init: function() {
			$(document.body).append('<div id="orgTreeRMenu" class="rMenu">' + '<div>添加到分组</div>' + '<ul>' + '</ul>' + '</div>');
		}
	}
})();